//去掉空格
String.prototype.trim = function(){   
	return this.replace(/^\s*|\s*$/g,"");
}

String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
    	if(reallyDo=="?") return this.replace(/\?/g,replaceWith);
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}

String.prototype.startWith=function(str){     
	var reg=new RegExp("^"+str);
	return reg.test(this);
} 
 
String.prototype.endWith=function(str){     
	var reg=new RegExp(str+"$");
	return reg.test(this);
}

String.prototype.encode=function(){
	return encodeURI(encodeURI(this));
}

Array.prototype.contains = function(item){
	return RegExp("(^|,)" + item.toString() + "($|,)").test(this);
};

//去掉数组中的重复项
Array.prototype.unique = function() { 
    var res = [], hash = {}; 
    for(var i=0, elem; (elem = this[i]) != null; i++) { 
        if (!hash[elem]) { 
            res.push(elem); 
            hash[elem] = true; 
        } 
    } 
    return res; 
}

//清空文本框的值
function clearText(theForm){
	if(theForm==undefined || theForm==null)
		theForm = document.forms[0];
    var el = theForm.elements;
    for(var i=0, m=el.length; i<m; i++){
        if(el[i].type=="text" || el[i].type=="textarea" || el[i].type=="select-one") 
        	el[i].value = "";
        else if(el[i].type=="checkbox" && el[i].checked)
        	el[i].checked=false;
    }
}

//限制文本的输入长度,注意汉字实际上占两个字符的位置，但在这里算做一个。
function textCount(field, maxlimit) {
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit);
		alert("最多可以输入"+maxlimit+"字符");
		field.focus();
		return false;
	}
}

// 检查表单是否为空
function checkNull(formElement,info) {
	var isNull = false;
	var obj = formElement;
	if (formElement != null) {
		if(typeof(formElement)=='string'){
			obj = document.forms[0][formElement];
		}
	    if (obj.value.trim() == "") {
	    	isNull=true;
	    	if(info!=null && info!=undefined){
	    		window.alert(info);
	    	}
	    	try{
	    		obj.focus();
	    	}catch(err){}
	    }
	}
	return isNull;
}

//获取表单默认值
function getDefaultVal(obj){
	var eleType = obj.type;
	if(eleType == "select-one"){
		var dropdown = obj.options;
		for (var i = dropdown.length-1; i >= 0; i--) {
			if (dropdown[i].defaultSelected) {
				return dropdown[i].value;
			}
	 	}
	}else{
		return obj.defaultValue;
	}
}

//获取复选框的值
function getCheckboxValue(name){
	var val="";
	var objs=document.getElementsByName(name);
	if(objs == null)
		return val;
	var len=objs.length;
	for(var i=0;i<len;i++) {
		if(objs[i].checked)
			val += ","+objs[i].value;
	}
	if(val!="" && 0<val.length){
		val=val.substring(1);
	}
	return val;
}
//设置复选框的值，选中状态
function setCheckboxValue(name,values){
	var objs=document.getElementsByName(name);
	if(objs == null || values=='') return;
	var len=objs.length;
	var valArr = strToArr(values);
	for(var i=0;i<len;i++) {
		if(valArr.contains(objs[i].value)){
			objs[i].checked = true;
		}else{
			objs[i].checked = false;	
		}
	}
}

//获取单选按钮的值
function getRadiosValue(name){
	var objs=document.getElementsByName(name);
	if(objs == null)
		return "";
	for(var i=0;i<objs.length;i++) {
		if(objs[i].checked) {
			return objs[i].value;
		}
	}
	return "";
}

//验证单选组是否选择
function checkRadiosIsSelected(_obj){
	var flag=false;
	var objs = _obj;
	if(typeof(_obj)=='string'){
		objs = document.getElementsByName(_obj);
	}
	if(objs==undefined || 0==objs.length)
		return true;
	for(var i=0;i<objs.length;i++) {
		if(objs[i].checked) {
			flag = true;
			break;
		}
	}
	return flag;
}

//设置复选框/单选框 全不选
function allRadiosSelect(name,flag){
	var objs=document.getElementsByName(name);
	for(var i=0;i<objs.length;i++) {
		if(null!=objs[i]) {
			objs[i].checked=flag;
			try{objs[i].fireEvent("onclick");}catch(e){}
		}
	}
}

//获取选择的下拉框的text
function getOptionText(obj){
	var val="";
	for(var i=0; i < obj.options.length; i++){
		if (obj.options[i].selected && obj.value!=""){
			val = obj.options[i].text;
			break;
		}
	}
	return val;
}

// 清空下拉框
function clearOpts(selectObj){
	if(selectObj == null) return;
	while(selectObj.childNodes.length>0){
		selectObj.removeChild(selectObj.childNodes[0]);
	}
    var option = null;
	option = document.createElement("option");
	option.appendChild(document.createTextNode("-- 请选择 --"));
	option.setAttribute("value","");
	selectObj.appendChild(option);
}

//失焦方法
function doManageValue(obj,str){
	var objNum = obj.value * 1;
	var patrn = /^\+?[0-9][0-9]*$/; //正整数
	if(str==2){//正浮点数
		var patrn = /^[0-9]+\.?[0-9]{0,3}$/; // 三位小数
		if(patrn.test(objNum)){
			obj.value = FormatNumber(obj.value,3); // 三位小数
		}
	}
	if (!patrn.test(objNum)) {
		alert("请输入有效数值！");
		obj.select();//.value="";
		return false;
	}
	return true;
}
/**
 * 小数加法运算
 * @param value1
 * @param value2
 * @return
 */
function addValue(value1,value2,i){  
    if(undefined==value1 || value1=="")value1="0";
    if(undefined==value2 || value2=="")value2="0";
    var temp1=0;
    var temp2=0;
    i = i!=undefined?i:1;
    if(value1.indexOf(".")!=-1)  
    	temp1=value1.length - value1.indexOf(".")-1;  
    if(value2.indexOf(".")!=-1)  
    	temp2=value2.length - value2.indexOf(".")-1;   
    var temp=0;  
    if(temp1>temp2)  
    	temp = (parseFloat(value1)+parseFloat(value2)*i).toFixed(temp1);  
    else 
    	temp = (parseFloat(value1)+parseFloat(value2)*i).toFixed(temp2);   
    return temp;  
}

function getFileExt(FileName){
	return FileName.substring(FileName.lastIndexOf('.')+1, FileName.length).toLowerCase(); 
}

function strToArr(str){
	var ret=[];
	if(null == str || 0==str.length) return ret;
	var arr = str.split(",");
	for(var i=0; i<arr.length; i++){
		var t = arr[i].trim();
		if(t.length > 0){
			ret.push(t);
		}
	}
	return ret;
}

function arrToStr(arr,type){
	var str = "";
	if(arr == undefined || arrToStr == null){
		return str;
	}
	for(var i=0; i<arr.length; i++){
		var t = arr[i].trim();
		if(type == 'string'){
			t = t.replaceAll("'","");
			t = "'"+t+"'";
		}
		str += t;
		if(i<arr.length-1){
			str += ",";
		}
	}
	return str;
}

//格式化文本内容
function fmtTxtContent(obj, regex){
	if(regex==undefined || regex==null){
		regex = "'";
	}
	var str = "";
	var bol = 0;
	var oldVal = obj.value.trim().replaceAll("，",",");
	if(oldVal!="" && regex=="'"){
		if(!oldVal.startWith("'") && !oldVal.endWith("'")){
			for(var n=0;n<oldVal.length;n++){
				if(oldVal.charCodeAt(n)==44 ||oldVal.charCodeAt(n)==32 || oldVal.charCodeAt(n)==13 || oldVal.charCodeAt(n)==10 || oldVal.charCodeAt(n)==9){
					if(bol == 0){
						str += "','";
						bol =1;
					}
					if(bol == 1){
						continue;
					}
				} else{
					str += oldVal.charAt(n).trim();
					bol = 0;
				}
			}
		}else{
			str = oldVal;
		}
		if(!str.startWith("'"))
			str = "'"+str;
		if(!str.endWith("'"))
			str = str+"'";
	}
	else{
		for(var n=0;n<oldVal.length;n++){
			if(oldVal.charCodeAt(n)==44 ||oldVal.charCodeAt(n)==32 || oldVal.charCodeAt(n)==13 || oldVal.charCodeAt(n)==10 || oldVal.charCodeAt(n)==9){
				if(bol == 0){
					str += regex+","+regex;
					bol =1;
				}
				if(bol == 1){
					continue;
				}
			} else{
				str += oldVal.charAt(n).trim();
				bol = 0;
			}
		}
	}
	obj.value = str;
}

function getOpener(){
	var callerWindowObj = window.dialogArguments;
	if(callerWindowObj != undefined){
		opener = callerWindowObj;
	}
	return opener;
}

/**
 * 解析URL中的参数
 * @param {url路径} string
 * @returns {返回object<key,value>}
 */
function getUrlParam(string) {
	var obj = new Object();
	if (string.indexOf("?") != -1) {
		var string = string.substr(string.indexOf("?") + 1);
		var strs = string.split("&");
		for (var i = 0; i < strs.length; i++) {
			var tempArr = strs[i].split("=");
			obj[tempArr[0]] = tempArr[1];
		}
	}
	return obj;
}
function getUrlParamStr() {
	var string = window.location.href;
	if (string.indexOf("?") != -1) {
		return string.substr(string.indexOf("?")+1);
	}
	return "";
}
/**
 * 窗口返回
 * @param url
 */
function winBack(url){
	var opener = getOpener();
	if(opener!=null && opener!=undefined){
		window.close();
	}else{
		if(url != undefined){
			location.href = url;
		}
		else{
			history.go(-1);
		}
	}
}

/***** 判断是否为移动设备 ******/
function isMobileAgent() {
	var sUserAgent = navigator.userAgent.toLowerCase();
	var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
	var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
	var bIsMidp = sUserAgent.match(/midp/i) == "midp";
	var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
	var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
	var bIsAndroid = sUserAgent.match(/android/i) == "android";
	var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
	var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
	if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
		return true;
	}
	return false;
}

/*
 *添加到收藏夹
 */
function AddFavorite(sURL, sTitle){
	sURL = sURL!=undefined?sURL:window.location;
	sTitle = sTitle!=undefined?sTitle:document.title;
	try {
		if (document.all) {
			window.external.addFavorite(sURL, sTitle);
		}
		else if (window.sidebar) {
			window.sidebar.addPanel(sTitle, sURL, "");
		}
	}catch(e){
		alert("加入收藏失败，请使用Ctrl+D进行添加");
	}
}

function getBasePath(){
	var locatUrl = (window.location+'').split('/');
	return locatUrl[0]+'//'+locatUrl[2]+'/'+locatUrl[3];
}



